import { createRouter, createWebHistory } from 'vue-router'
/* 配置路由的第一种方式：导入对应的组件 不作推荐 */
// import HomeView from '../views/HomeView.vue'
// import index from '../views/layout/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      /* 第一种配置方式：直接使用导入的组件名称 推荐使用箭头函数配置方式*/
      path: '/',
      name: 'home',
      component: () => import('@/views/layout/index.vue'),
      /* 重定向 */
      redirect: '/index',
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/index/index.vue'),
        },
        {
          path: 'clazz',
          name: 'clazz',
          component: () => import('@/views/clazz/index.vue'),
        },
        {
          path: 'stu',
          name: 'stu',
          component: () => import('../views/stu/index.vue'),
        },
        {
          path: 'emp',
          name: 'emp',
          component: () => import('../views/emp/index.vue'),
        },
        {
          path: 'dept',
          name: 'dept',
          component: () => import('../views/dept/index.vue'),
        },
        // 统计管理页面放到主布局下
        {
          path: 'stats/employee',
          name: 'empCount',
          component: () => import('@/views/emp/empCount.vue'),
        },
        {
          path: 'stats/student',
          name: 'stuCount',
          component: () => import('@/views/stu/stuCount.vue'),
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
    },
  ],
})

// 全局前置守卫：未登录强制跳转到登录页
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  const userInfo = localStorage.getItem('userInfo')
  if (!userInfo) {
    return next('/login')
  }
  next()
})

export default router
